﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête direct_messages</summary>
    /// <see cref="DirectMessages"></see>
    [DataContract]
    public class direct_messages
    {

        /// <summary>id du message</summary>
        [DataMember]
        public int id { get; set; }

        /// <summary>texte du message</summary>
        [DataMember]
        public string text { get; set; }

        /// <summary>ID de l’expéditeur</summary>
        [DataMember]
        public int sender_id { get; set; }

        /// <summary>ID du destinataire</summary>
        [DataMember]
        public int recipient_id { get; set; }

        /// <summary>date du message au format suivant : Tue Sep 16 11:38:39 +0200 2008 </summary>
        /// <remarks>en PHP : date(D M j H:i:s O Y</remarks>
        [DataMember]
        public string created_at { get; set; }

        /// <summary>login de l’expéditeur suivi de sa famille</summary>
        [DataMember]
        public string sender_screen_name { get; set; }

        /// <summary>login du destinataire suivi de sa famille</summary>
        [DataMember]
        public string recipient_screen_name { get; set; }

        /// <summary>expéditeur</summary>        
        [DataMember]
        public user sender { get; set; }

        /// <summary>destinataire</summary>                
        [DataMember]
        public user recipient { get; set; }
    }

    public delegate void DirectMessagesEventHandler(object sender, DirectMessagesEventArgs e);
    public class DirectMessagesEventArgs : EventArgs
    {
        public DirectMessagesEventArgs(ObservableCollection<direct_messages> dm)
        {
            _result = dm;
        }

        private ObservableCollection<direct_messages> _result;
        public ObservableCollection<direct_messages> result { get { return _result; } }
    }

    public partial class apitxt : api
    {

        public event DirectMessagesEventHandler DirectMessagesCompleted;

        #region Direct Messages
        /// <summary>Permet de récupérer la liste des 20 derniers messages privés de l’utilisateur authentifié toutes familles confondues.</summary>
        /// <c>http://apitxt.hellotipi.com/direct_messages.format</c>
        /// <param name="since">limite les résultats aux messages postés après cette date ; le même comprotement peut être obtenu en spécifiant l’entête HTTP If-Modified-Since dans la requête. Date au format RFC 2822</param>
        /// <param name="since_id">renvoie les messages postés après le message since_id</param>
        /// <param name="page">numéro de la page demandée</param>
        public void DirectMessages(DateTime? since = null, int? since_id = null, int? page = null)
        {
            base.APICallCompleted += new APICallCompletedEventHandler(DirectMessagesCallCompleted);

            var querystring = new List<KeyValuePair<string, string>> { };

            if (since != null)
                querystring.Add(new KeyValuePair<string, string>("since", since.ToString()));

            else if (since_id != null)
                querystring.Add(new KeyValuePair<string, string>("since_id", since_id.ToString()));

            else if (page != null)
                querystring.Add(new KeyValuePair<string, string>("page", since.ToString()));

            GetUrlApi("direct_messages.json", querystring);
        }

        void DirectMessagesCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                DirectMessagesCompleted.Invoke(sender, new DirectMessagesEventArgs(serializeTools.Deserialize<ObservableCollection<direct_messages>>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(DirectMessagesCallCompleted);
            }
        }

        #endregion
    }

}

